package com.example.modules.common.utils;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * json与obj转换
 * 
 * @author zbq
 * @version 1.0
 */
public class JSONUtil {
	private static ObjectMapper mapper = new ObjectMapper();

	/**
	 * obj转换为String
	 * 
	 * @param object
	 * @return String
	 */
	public static String toJson(Object object) {
		try {
			return mapper.writeValueAsString(object);
		} catch (IOException e) {
			throw new RuntimeException("object to json error!", e);
		}
	}

	/**
	 * json转换为obj
	 * 
	 * @param json
	 *            要转换的json串
	 * @param clazz
	 *            转换后对象
	 * @return T
	 */
	public static <T> T toObject(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			throw new RuntimeException("render json to object error!", e);
		}
	}

	/**
	 * json转换为集合对象
	 * 
	 * @param json
	 *            要转换的json串
	 * @param clazz
	 *            转换后对象
	 * @return List<T>
	 */
	public static <T> List<T> toObjectList(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, mapper.getTypeFactory().constructParametricType(ArrayList.class, clazz));
		} catch (Exception e) {
			throw new RuntimeException("from Json发生错误", e);
		}
	}
	/**
     * Map转换为JSON字符串
     *
     * @param map 需要转换的Map
     * @return
	 * @throws Exception 
     */
    public static <E, V> String map2JsonString(Map<E, V> map) throws Exception {
        try {
            ObjectMapper objectMapper =  instanceObjectMapper();
            if (map == null || map.isEmpty()) return EMPTY_JSON_STRING;
            return objectMapper.writeValueAsString(map);
        } catch (JsonParseException e) {
            throw new Exception(e.getMessage(), e);
        } catch (JsonMappingException e) {
            throw new Exception(e.getMessage(), e);
        } catch (IOException e) {
            throw new Exception(e.getMessage(), e);
        }
    }


    /**
     * Object转换为JSON字符串
     *
     * @param obj
     * @return
     * @Description
     * @author caobin
     * @throws Exception 
     */
    public static String obj2JsonString(Object obj) throws Exception {
        try {
            ObjectMapper objectMapper = instanceObjectMapper();
            if (obj == null) return EMPTY_JSON_STRING;
            return objectMapper.writeValueAsString(obj);
        } catch (JsonParseException e) {
            throw new Exception(e.getMessage(), e);
        } catch (JsonMappingException e) {
            throw new Exception(e.getMessage(), e);
        } catch (IOException e) {
            throw new Exception(e.getMessage(), e);
        }
    }


    /**
     * JSON字符串转换为Map
     *
     * @param strJsonString 需要转换的JSON字符串
     * @return
     * @throws Exception 
     */
    @SuppressWarnings("unchecked")
    public static <E, V> Map<E, V> jsonString2Map(String strJsonString) throws Exception {
        try {
            ObjectMapper objectMapper = instanceObjectMapper();
            if (StringUtils.isEmpty(strJsonString)) strJsonString = EMPTY_JSON_STRING;
            return objectMapper.readValue(strJsonString, Map.class);
        } catch (JsonParseException e) {
            throw new Exception(e.getMessage(), e);
        } catch (JsonMappingException e) {
            throw new Exception(e.getMessage(), e);
        } catch (IOException e) {
            throw new Exception(e.getMessage(), e);
        }
    }

    /**
     * JSON字符串转换为Object
     *
     * @param strJsonString 需要转换的JSON字符串
     * @param clazz
     * @return
     * @throws Exception 
     */
    public static <T> T jsonString2Object(String strJsonString, Class<T> clazz) throws Exception {
        try {
            ObjectMapper objectMapper = instanceObjectMapper();
            if (StringUtils.isEmpty(strJsonString)) strJsonString = EMPTY_JSON_STRING;
            return objectMapper.readValue(strJsonString, clazz);
        } catch (JsonParseException e) {
            throw new Exception(e.getMessage(), e);
        } catch (JsonMappingException e) {
            throw new Exception(e.getMessage(), e);
        } catch (IOException e) {
            throw new Exception(e.getMessage(), e);
        }
    }
    
    /**
     * 实例化ObjectMapper
     *
     * @return
     */
    public static ObjectMapper instanceObjectMapper() {
        JsonFactory jf = new JsonFactory();
//        jf.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
        return new ObjectMapper(jf);
    }

    /**
     * 空JSON字符串
     */
    private final static String EMPTY_JSON_STRING = "{}";
	
	
}
